﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class QuotationService : EntityService<Quotation>
    {
        public List<Quotation> GetAllQuotations()
        {
            return (from quotation in this.MongoConnectionHandler.MongoCollection.FindAllAs<Quotation>()
                    orderby quotation.CompanyType ascending
                    select quotation).ToList();
        }

        public Quotation GetQuotationOrderbySeqNo_ByQuoteNo(String QuotationNo)
        {
            return (from quotation in this.MongoConnectionHandler.MongoCollection.FindAllAs<Quotation>()
                    where quotation.QuotationNo == QuotationNo
                    orderby quotation.SequenceNo ascending
                    select quotation).FirstOrDefault();
        }

        public List<Quotation> GetOrderQuotation_ByServiceNO(String ServiceNo)
        {
            return (from quotation in this.MongoConnectionHandler.MongoCollection.FindAllAs<Quotation>()
                    where quotation.ServiceOrderNo == ServiceNo
                    select quotation).ToList();
        }

        public List<Quotation> GetOrderQuotation_ByQuotationNo(String QuotationNo)
        {
            return (from quotation in this.MongoConnectionHandler.MongoCollection.FindAllAs<Quotation>()
                    where quotation.QuotationNo == QuotationNo
                    orderby quotation.SequenceNo
                    select quotation).ToList();
        }

        public bool DeleteQuotations(string QuotationNo)
        {
            bool isSuccess = false;

            try
            {
                var Query = Query<Quotation>.EQ(e => e.QuotationNo, QuotationNo);
                this.MongoConnectionHandler.MongoCollection.Remove(Query);

                isSuccess = true;
            }
            catch(Exception e)
            {
                isSuccess = false;
            }

            return isSuccess;
        }

        public bool CreateQuotations(List<Quotation> orderQuotationList)
        {
            bool isSuccessful = false;

            try
            {
                foreach (Quotation quotation in orderQuotationList)
                {
                    this.Create(quotation);
                }

                isSuccessful = true;
            }
            catch (Exception e)
            {
                isSuccessful = false;
            }

            return isSuccessful;
        }

        public void UpdatePONo(String QuotationNo, String PONo)
        {
            List<Quotation> QuotationList = GetOrderQuotation_ByQuotationNo(QuotationNo);

            if (QuotationList.Count > 0)
            {
                foreach (Quotation item in QuotationList)
                {
                    var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                        Query<Quotation>.EQ(o => o.QuotationNo, item.QuotationNo),
                        Update<Quotation>.Set(o => o.PONo, PONo));
                }
            }


        }

        public override void Update(Quotation entity)
        {
            List<Quotation> QuotationList = GetOrderQuotation_ByQuotationNo(entity.QuotationNo);

            if (QuotationList.Count > 0)
            {
                foreach (Quotation item in QuotationList)
                {
                    var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                        Query<Quotation>.EQ(o => o.QuotationNo, item.QuotationNo),
                        Update<Quotation>.Set(o => o.PDQuotationNo, item.PDQuotationNo)
                        .Set(o => o.PDInvoiceNo, item.PDInvoiceNo)
                        .Set(o => o.InvoicePayment, item.InvoicePayment)
                        .Set(o => o.PaymentRef, item.PaymentRef)
                        .Set(o => o.ReceivedDate, item.ReceivedDate)
                        .Set(o => o.RefNo, item.RefNo)
                        .Set(o => o.PurchaseOrderNo, item.PurchaseOrderNo));
                }
            }
        }
    }
}